package xyz.hzxiao.controller;

import cn.novelweb.tool.upload.local.pojo.UploadFileParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;
import xyz.hzxiao.pojo.UploadSuccessVO;
import xyz.hzxiao.service.FileUploadService;
import xyz.hzxiao.utils.AppResult;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * 文件上传控制器
 *
 * @author ：FuXx
 * @className : xyz.hzxiao.controller.FileUploadController
 * @since ：2023/3/7
 */
@RestController
@RequestMapping("/file")
public class FileUploadController {

    private static final Logger LOGGER = LoggerFactory.getLogger(FileUploadController.class);

    @Resource(name = "fileUploadServiceImpl")
    private FileUploadService currentService;


    /**
     * 检查文件MD5（文件MD5若已存在进行秒传）
     *
     * @param fileName 文件名称
     * @param md5      文件md5
     */
    @GetMapping("/fileMd5")
    public AppResult<?> checkFileMd5(String md5, String fileName) throws Exception {
        return currentService.checkFileMd5(md5, fileName);
    }


    /**
     * 断点续传（分片）方式上传文件：用于大文件上传
     *
     * @param param   参数
     * @param request 请求对象
     */
    @PostMapping(value = "/shardingUpload")
    public AppResult<?> shardingUploadFile(UploadFileParam param, HttpServletRequest request) throws Exception {
        LOGGER.info("分片上传：总分片数：{}，本次上传第{}分片，本次分片大小{}", param.getChunks(), param.getChunk(), param.getFile().getSize());
        return currentService.shardingUploadFile(param, request);
    }


    /**
     * 分片上传完成后对文件的操作，参数可以按业务自定义
     * 这里单独去原文件名称
     *
     * @return 操作完成
     */
    @PostMapping("/uploadSuccess")
    public AppResult<String> uploadSuccess(@RequestBody UploadSuccessVO vo) {
        return currentService.uploadSuccess(vo);
    }
}
